//
//  YAYIRefundsController.m
//  YAYIDoctor
//
//  Created by hxp on 16/6/13.
//
//

#import "YAYIRefundsController.h"
#import "YAYINoPayOrderModel.h"
#import "YAYIRefundsCell.h"
#import "YAYIOrderRefundsController.h"
#import "YAYIRefundUnfinishedCell.h"
#import "YAYIOrderdetailController.h"


@interface YAYIRefundsController ()

// 没有数据显示提示
@property (nonatomic, weak) UILabel *noDataFlagLbl;

@property (nonatomic,strong)NSMutableArray *dataAry;

/** 分页相关 */
@property (nonatomic, assign) NSInteger curPage;
@property (nonatomic, assign) NSInteger pageCount;

@end

@implementation YAYIRefundsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataAry = [NSMutableArray array];
    [self createDataSource];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(108, 0, 20, 0);
    
    // 上拉下拉刷新
    __block typeof(self) weakself = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakself createDataSource];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakself getMoreOrder];
    }];
//    [self.tableView.mj_footer setAutomaticallyHidden:YES];
    
    // 进入执行下拉
    [self.tableView.mj_header beginRefreshing];
}


/**
 *  没有数据时显示
 */
- (void)showNoDataFlag:(BOOL)flag
{
    if (!flag) {
        [self.noDataFlagLbl removeFromSuperview];
        self.noDataFlagLbl = nil;
        return;
    }
    
    if (self.noDataFlagLbl) {
        return;
    }
    UILabel *flagLbl = [[UILabel alloc] init];
    flagLbl.text = @"暂时没有数据";
    flagLbl.font = [UIFont systemFontOfSize:15];
    flagLbl.textColor = YAYIColor(107, 178, 242);
    flagLbl.textAlignment = NSTextAlignmentCenter;
    [self.tableView addSubview:flagLbl];
    self.noDataFlagLbl = flagLbl;
    [flagLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView);
        make.centerY.equalTo(self.tableView).offset(-90);
    }];
}


-(void)createDataSource
{
    
    [self.dataAry removeAllObjects];
    
    [YAYINetWorking  getWithURL:refundOrder_list_url param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        NSString *status = dict[@"status"];
        
        YAYILog(@"dict:%@", dict);
        
        if ([status isEqualToString:@"success"]) {
            
            NSArray *data = dict[@"data"];
            for (NSDictionary *obj in data) {
                YAYINoPayOrderModel *model = [YAYINoPayOrderModel new];
                [model setValuesForKeysWithDictionary:obj];
                [self.dataAry  addObject:model];
            }
            
            self.curPage = 1;
            [self.tableView.mj_header endRefreshing];
            if (self.dataAry.count < 10) {
                
                [self.tableView.mj_footer endRefreshing];
                self.tableView.mj_footer.hidden = YES;
            }else{
                [self.tableView.mj_footer setState:MJRefreshStateIdle];
                self.tableView.mj_footer.hidden = NO;
            }
            
            [self.tableView  reloadData];
        }
        
        if (0 == self.dataAry.count) {
            [self showNoDataFlag:YES];
            
        }else{
            [self showNoDataFlag:NO];
        }
        
        [self.tableView.mj_header endRefreshing];
    }];
}


-(void)getMoreOrder
{
    self.curPage++;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"currentPage"] = @(self.curPage);
    
    [YAYINetWorking  getWithURL:refundOrder_list_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        //NSLog(@"%@",dict);
        YAYILog(@"返回：%@", dict);
        
        NSString *status = dict[@"status"];
        if ([status isEqualToString:@"success"]) {
            NSArray *data = dict[@"data"];
            for (NSDictionary *obj in data) {
                YAYINoPayOrderModel *model = [YAYINoPayOrderModel new];
                [model setValuesForKeysWithDictionary:obj];
                [self.dataAry  addObject:model];
            }
            
            if (data.count < 10) {
                [self.tableView.mj_footer endRefreshing];
                self.tableView.mj_footer.hidden = YES;
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
            
            [self.tableView  reloadData];
        }
    }];
}


#pragma mark - 点击事件
-(void)onRefundBtnClick:(UIButton *)sender
{
    YAYIOrderRefundsController *order = [YAYIOrderRefundsController new];
    order.pageType = PageTypeRedund;
    order.title = @"订单退款";
    
    YAYINoPayOrderModel *model = self.dataAry[sender.tag];
    order.avatar = model.userAvatar;
    order.patientname = model.patientname;
    order.clientname = model.userfullname;
    order.mobile  = model.patientMobile;
    order.totalPrice = model.totalprice;
    order.reduce = model.reduce;
    order.orderid = model.id;
    order.userid = model.userid;
    
    __block typeof(self) weakself = self;
    order.refreshOperation = ^(){
        
        [weakself.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:order animated:YES];
    
}


#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    YAYINoPayOrderModel *model = self.dataAry[indexPath.row];
    if (model.canRefund.integerValue == 1) {
        
        YAYIRefundUnfinishedCell *cell = [YAYIRefundUnfinishedCell cellWith:tableView];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
        cell.model = self.dataAry[indexPath.row];
        
        cell.button.tag = indexPath.row;
        [cell.button addTarget:self action:@selector(onRefundBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }else{
    
        YAYIRefundsCell *cell = [YAYIRefundsCell cellWith:tableView];

        cell.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.dataAry[indexPath.row];
        return cell;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataAry.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    YAYINoPayOrderModel *model = self.dataAry[indexPath.row];
    if (model.canRefund.integerValue == 1) {
        return 244;
    }else{
        return 197;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YAYINoPayOrderModel *model = self.dataAry[indexPath.row];
    
//    YAYIOrderRefundsController *refundVc = [[YAYIOrderRefundsController alloc] init];
//    refundVc.pageType = PageTypeDetail;
    
//    refundVc.title = @"退款详情";
//    refundVc.noPayModel = model;
//    refundVc.patientname = model.patientname;
//    refundVc.hospital = model.hospitalname;
//    refundVc.clientname = model.userfullname;
//    refundVc.mobile = model.userMobile;
//    refundVc.orderid = model.id;
//    refundVc.totalPrice = model.totalprice;
//    refundVc.reduce = model.reduce;
//    refundVc.userid = model.userid;
//    refundVc.avatar = model.userAvatar;
    
    YAYIOrderdetailController *refundDetailVc = [[YAYIOrderdetailController alloc] init];
    refundDetailVc.isRefund = YES;
    
    refundDetailVc.noPayModel = model;
    
    refundDetailVc.title = @"收费详情";
    refundDetailVc.orderid = model.id;
    refundDetailVc.patientname = model.patientname;
    refundDetailVc.clientname = model.userfullname;
    refundDetailVc.avatar = [NSString stringWithFormat:@"%@%@", API, model.userAvatar];
    refundDetailVc.mobile = model.patientMobile;
    refundDetailVc.hospital = model.hospitalname;
    refundDetailVc.totalPrice = model.totalprice;
    refundDetailVc.state = model.state;
    refundDetailVc.reducePrice = [NSString stringWithFormat:@"%d",([model.totalprice intValue] - [model.reduce intValue])];
    refundDetailVc.reduce = model.reduce;
    refundDetailVc.userid = model.userid;
    
    [self.navigationController pushViewController:refundDetailVc animated:YES];
}


@end
